﻿package org.xhs.lib.utils
{

    public class NumberFormatter extends Object
    {

        public function NumberFormatter()
        {
            return;
        }// end function

        public static function format(param1:Number, param2:String) : String
        {
            var str:String;
            var intStr:String;
            var decStr:String;
            var num:* = param1;
            var formatStr:* = param2;
            var numStr:* = num.toString();
            var tempAry:* = numStr.split(".");
            intStr = tempAry[0];
            decStr = tempAry.length > 1 ? (tempAry[1]) : ("");
            str = formatStr.replace(/I+,*I*""I+,*I*/g, function () : String
            {
                var _loc_3:* = undefined;
                var _loc_5:* = undefined;
                var _loc_6:* = undefined;
                arguments = arguments[0] as String;
                var _loc_4:* = arguments.lastIndexOf(",");
                if (arguments.lastIndexOf(",") >= 0 && _loc_4 != (intStr.length - 1))
                {
                    _loc_5 = (arguments.length - 1) - _loc_4;
                    _loc_6 = [];
                    while (intStr.length > _loc_5)
                    {
                        
                        _loc_6.push(intStr.substr(-_loc_5));
                        intStr = intStr.substring(0, intStr.length - _loc_5);
                    }
                    _loc_6.push(intStr);
                    _loc_6.reverse();
                    if (_loc_6[0] == "-")
                    {
                        _loc_6.shift();
                        _loc_3 = "-" + _loc_6.join(",");
                    }
                    else
                    {
                        _loc_3 = _loc_6.join(",");
                    }
                }
                else
                {
                    _loc_3 = intStr;
                }
                return _loc_3;
            }// end function
            );
            str = str.replace(/D+""D+/g, function () : String
            {
                arguments = arguments[0] as String;
                var _loc_3:* = decStr;
                if (_loc_3.length > arguments.length)
                {
                    _loc_3 = _loc_3.substr(0, arguments.length);
                }
                else
                {
                    while (_loc_3.length < arguments.length)
                    {
                        
                        _loc_3 = _loc_3 + "0";
                    }
                }
                return _loc_3;
            }// end function
            );
            return str;
        }// end function

    }
}
